--
-- Author: yawei.zhu
-- Date: 2019-01-21 11:08:54
--
-- PvpChallengeActivitySuccessView
--
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ActivityWinScene

local rewardNum = 3 -- 奖励个数
local gradeStr = {
	string.format(" (%s)", L("copper_cup")),
	string.format(" (%s)", L("silver_cup")),
	string.format(" (%s)", L("gold_cup"))
}

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		self:closeView()
	end)

	self._iconRenders = {}
	for i = 1, rewardNum do
		local render = IconRender.create(self._ui["icon_"..i])
		render:setTipEnable(true)
		self._iconRenders[i] = render
	end
end

function ClassRef:refreshContent(playId, grade, score, itemDatas, dailyScore, winTimes)
	local eventModeConf = GD:queryEventModeById(playId)
	local evaluateType = tonumber( eventModeConf.evaluateType )

	local realGrade = ""
	local scoreTemp = tonumber(score or 0)

	-- 1=高于限定回合
	-- 2=小于限定时间
	-- 3=高于限定血量
	-- 4=灰烬碎片数量
	-- 5=失败方格子数
	if 1 == evaluateType then -- 狮心号角：回合数
		realGrade = string.format( L("LionNotice"), scoreTemp )
	elseif 2 == evaluateType then
		realGrade = string.format( L("ZombieNotice"), scoreTemp )
	elseif 3 == evaluateType then
		realGrade = string.format( L("KingofFightersNotice"), scoreTemp )
	elseif 4 == evaluateType then
		realGrade = string.format( L("lua_code_text_407"), scoreTemp )
	elseif 5 == evaluateType then
		realGrade = string.format( L("ChessWinNotice"), scoreTemp )
	else
		print( " ----------------->>>> 未知活动副本类型 " )
	end

	local str = gradeStr[grade]
	if not str then
		print("Error: 挑战赛活动结算出错！服务器发送grade = ", grade)
		str = ""
	end
	realGrade = realGrade .. str
	-- 通关结果
	self._ui.tips_1:setString(realGrade)

	-- 通关奖励
	local intervalX = 112
	local offsetX = 38
	local midX = 255
	local count = #itemDatas
	if count > rewardNum then count = rewardNum end
	local midNum = (count + 1) / 2
	for i = 1, rewardNum do
		local render = self._iconRenders[i]
		local data = itemDatas[i]
		if data then
			render:setVisible(true)
			render:setData(data)
			render:setPositionX(midX - (midNum - i) * intervalX - offsetX)
		else
			render:setVisible(false)
		end
	end

	-- 通关奖杯
	local iconPath = table.concat( { Res.teamUIPath, "team_trophy_icon_", (4 - grade), ".png" } )
	self._ui.icon:loadTexture( iconPath, 1 )

	-- 挑战赛星章数上限，胜场数上限(临界条件：打完这一场正好达到上限时候要显示奖励不显示tips，所以显示tips的前提条件是没有获得奖励)
	if count <= 0 then
		local config = GD:getChallengeInfo(playId)
		local maxWinTimes = config and tonumber(config.winNumMax) or 0
		local maxScore = tonumber( GD:getSystemPar( "challengeGameMaxDailyBadge", 100) )
		local allFlag = (dailyScore >= maxScore and winTimes >= maxWinTimes) or false
		local starCountFlag = dailyScore >= maxScore or false
		if allFlag then
			self._ui.text:setString(L("challengeTips20"))
		elseif starCountFlag then
			self._ui.text:setString(L("challengeTips19"))
		end
		self._ui.tips_challenge:setVisible(allFlag or starCountFlag)
		self._ui.tips_img:setVisible(not (allFlag or starCountFlag))
		self._ui.tips_2:setVisible(not (allFlag or starCountFlag))
	end
end

return ClassRef
